<include file="Public/header"/>
<include file="Public/menu"/>
<style type="text/css">
    .one-produc{
        width:780px;
        white-space: nowrap;
        position: relative;
        overflow:hidden;
        display: block;
    }
    .one-produc .text{
        width:170px;
        display:inline-block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: middle;
    }
    .one-produc .sgprice,.one-produc .geshu{
        width:58px;
    }
    .todo-list > li .tools{
        position:absolute;
        right:0px;
		top:14px;
    }
	.one-produc .label{font-size:12px!important;}
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            店铺营销
            <small>限时抢购</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Flashsales/index')}"><i class="fa fa-dashboard"></i> 店铺营销</a></li>
            <li class="active">限时抢购</li>
        </ol>
    </section>
    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">修改限时抢购信息</h3>
                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="{:U('Flashsales/timeIndex',['special_id' => $special_id])}" class="btn btn-sm btn-default"><i
                                        class="fa fa-list"></i>&nbsp;限时抢购时间段列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="form" action="{:U('Flashsales/save_rule')}" method="post" class="form-horizontal" onSubmit="return checkSubmit();">
                        <input value="{$msg.tid}" type="hidden" name="tid" />
                        <input value="{$special_id}" type="hidden" name="special_id" />
                        <div class="box-body">
                            <div class="form-group">
                                <div class="col-sm-2"></div>
                                <div class="input-group col-sm-6">
                                    <div class="alert alert-warning col-sm-6" role="alert" style="background-color: #fcf8e3 !important;border-color: #faebcc;color: #FF0000 !important;">
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <strong>警告!</strong> 请在添加商品之前，选择活动时间段
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">开始时间</label>
                                <div class="input-group col-sm-6">
                                	<div class="input-group no-margin">
                                    	<input id="begin_time" name="begin_time" class="form-control" type="text" value="{$msg.begin_time}" readonly/>
                                    </div>
                                </div>                                
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">结束时间</label>
                                <div class="input-group col-sm-6">
                                	<div class="input-group no-margin">
                                    	<input id="end_time" id="end_time" name="end_time" class="form-control" type="text" value="{$msg.end_time}" readonly/>
                                    </div>
                                </div>                                
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">限购数量</label>
                                <div class="input-group col-sm-6">
                                    <div class="no-margin">
                                        <input name="limit" class="form-control ajax-sort"  value="{$msg.limit}" type="text" placeholder="正整数">
                                    </div>
                                </div>
                            </div>

                            <!--<div class="form-group">
                                <label class="col-sm-2 control-label">状态</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input class="form-control ajax-sort" value="{$msg.stat}"></li>

                                    </div>
                                </div>
                            </div>-->

                            <!--{:dump($msg[p])}-->
                            <div class="form-group set-goods">
                                <label class="col-sm-2 control-label">设置商品</label>
                                <div class="input-group col-sm-6">
                                    <div class="no-margin">
                                            <?php if(!isset($msg['p']) || $msg['p']){?>
                                            <button type="button" class="btn btn-default pull-left" id="daterange-btn">
                                                <span>
                                                  <i class="fa fa-calendar"></i> 点击添加商品
                                                </span>
                                                <i class="fa fa-caret-down"></i>
                                            </button>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group set-goods">
                                <div class="col-sm-offset-2 input-group col-sm-6">
                                    <div class="box box-primary" style="width:800px;">
                                        <!-- /.box-header -->
                                        <div class="box-body">
                                            <ul class="todo-list ui-sortable">
                                                <volist name="msg[products]" id="p">
                                                    <li class="one-produc">
                                                        <input type="hidden" name="store_pid[]" value="{$p.store_pid}">
                                                        <!--<input type="hidden" name="goods[tid][]" value="{$p.tid}">
                                                        <input type="hidden" name="goods[special_id][]" value="{$p.special_id}">-->
                                                        <a class="text" href="javascript:;" title="{$p.p_name}">{$p.p_name}</a>
                                                        <small class="label label-warning"> 原价 : {$p[price]}</small>
                                                        限时抢购价 : <input type="text" class="sgprice"  data-price="{$p[price]}" onblur="validateShanGou(this, 'sg-cost');" name="promotion_price[]" value="{$p.promotion_price}">
                                                        <small class="label label-success"> 可用库存 : {$p[store]-$p[freez]+$p[initnum]}</small>
                                                        货品数量 : <span class="text shuliang"><input type="text"  data-num="{$p[store]-$p[freez]+$p[initnum]}" class="geshu" name="initnum[]" value="{$p.initnum}" onblur="validateShanGou(this, 'gd-num');"/></span>
                                                        <div class="tools">
                                                            <i class="fa fa-trash-o icondel" data-id="{$p.store_pid}" onclick="deleteProduct(this)"></i>
                                                        </div>
                                                    </li>
                                                </volist>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group set-goods">
                                <label class="col-sm-2 control-label">&nbsp;&nbsp;</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <input id="yc-goods-ids" name="product_ids" value="{$s_pids}" type="hidden"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="box-footer">
                            <div class="col-sm-2">
                            </div>
                            <div class="col-sm-6">
                                <div class="btn-group pull-right">
                                    <button type="submit" class="btn btn-info pull-right submit_click">提交</button>
                                </div>
                            </div>
                        </div>
                </form>
        </div>

    </div>
</div>

</section>
</div>


<!-- 底部 -->
<include file="Public/common_footer"/>
<script type="text/javascript" src="/Public/layer/layer.js"></script>
<link rel="stylesheet" href="/Public/admin/jedate/jedate.css" />
<script src="/Public/admin/jedate/jedate.js"></script>
<script type="text/javascript">
    // 设置时间
    function setJedate(){
        //不显示秒的解决办法
        var stxt = $("#begin_time").val().trim();
        if(stxt.length == 19){
            $("#begin_time").val(stxt.substr(0, 16));
        }
        var etxt = $("#end_time").val().trim();
        if(etxt.length == 19){
            $("#end_time").val(etxt.substr(0, 16));
        }
		var nowStr = getNowDateStr();
		// 获取当前时间
        var start = {
            dateCell: '#begin_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: nowStr + ' 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                end.minDate = datas; 
            }
        };
        var end = {
            dateCell: '#end_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: nowStr + ' 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                start.maxDate = datas;
            }
        };
        jeDate(start);
        jeDate(end);
    }
    setJedate();
    $(function(){
        var _status = '{$msg.p}';
        //alert(_status);
        if(_status !='' && _status == 0){
            $('input').attr('disabled','disabled');
        }
    });
    //验证数据
    function validateShanGou(o, mark){
		return;
        var flag = true;
        if(mark == "sg-cost"){
            if(!mbValidator.isMoney(o.value)){
				o.value = "";
                flag = false;
            }else if(parseFloat(o.value) <= 0){
				o.value = "";
                flag = false;
			}else if(parseFloat(o.value) > parseFloat($(o).attr("data-price"))){
                o.value = $(o).attr("data-price");
                flag = false;
            }
        }else if(mark == 'gd-num'){
            var re = /^[1-9]\d*?$/g;
            if(!re.test(o.value)){
				o.value = 1;
                flag = false;
            }
            if(o.value > $(o).data('num')){
				o.value = $(o).data('num');
                flag = false;
            }
        }
    }
    var ychser = new YcChoser();
    var chids = $("#yc-goods-ids").val();
    ychser.setChosenIds(chids);

    function deleteProduct(o) {
		ychser.isRepeat($(o).attr("data-id"));
        ychser.remove();
		updateGoodsIds(ychser.getChosenIds());
		$(o).parents("li").remove();
    }
    function updateGoodsIds(ids) {
        $("#yc-goods-ids").val(ids);
        ychser.setChosenIds(ids);
    }
    //点击选择商品

    $('#daterange-btn').click(function () {
        var beginTime = $('#begin_time').val(), endTime = $("#end_time").val();
//        thisDate = thisDate.substr(0, 10);
//        alert(thisDate);
        layer.open({
            type: 2,
            title: '添加店铺商品',
            area: ['1400px', '830px'],
            fixed: false,
            maxmin: true,
            content: "{:U('Flashsales/getProducts',array('product_ids'=>$s_pids))}?beginTime="+beginTime+"&endTime"+endTime
        });
    });
	var checkSubmitFlag = false;
	// 检查提交时候的商品数据
	function checkSubmit(){
		checkSubmitFlag = true;
		$(".one-produc").each(function(){			
			var rs = "";
			var o = $(this).find(".sgprice");
			var curVal;
			var tmpFlag = false;
			var tmpVal;
			mbValidator.fixPrice(o, o.val());
			curVal = parseFloat(o.val());
			tmpVal = o.attr("data-price");
			if(curVal < 0.01){
				rs += "闪购价必须大于等于0.01";
			}else if(curVal > parseFloat(tmpVal)){
				rs += "闪购价不能大于原价";
			}
			o = $(this).find(".geshu");
			tmpVal = parseInt(o.attr("data-num"));
			if(tmpVal <= 0){
				rs = "可用库存不足，请移除掉该商品！";
			}else if(!mbValidator.isPositiveInt(o.val())){
				tmpVal = tmpVal;
				rs += (rs == "" ? "" : "，") + "货品数量必须为大于0的整数";
			}else if(parseInt(o.val()) > tmpVal){
				rs += (rs == "" ? "" : "，") + "货品数量不能大于可用库存";
			}
			if(rs == ""){
				$(this).find(".err-holder").remove();
			}else{
				checkSubmitFlag = false;
				$(this).find(".err-holder").remove();
				$(this).append('<div class="err-holder"><span class="error">'+rs+'</span></div>');
			}
			
		});
		return checkSubmitFlag;
	}
	//表单验证
    $("#form").validate({
		ignore: "",
        rules: {
			begin_time:{
				required: true,
				isActTime: true
			},
			end_time:{
				required: true,
				isActTime: true,
				dateTimeBigThan: "#begin_time"
			},
            limit:{
				required: true,
                isPositiveInt: true
            },
			product_ids:{
				required: true	
			}
        },
        messages:{
			begin_time:{
				required: "请输入开始时间"
			},
			end_time:{
				required: "请输入结束时间",
				dateTimeBigThan: "【注意：】结束时间不能小于开始时间"
			},
            limit: {
                required: "请输入限购数量"
            },
			product_ids:{
				required: "请至少选择一个商品"	
			}
        },
        submitHandler: function(form){
			if(checkSubmitFlag){
				form.submit();	
			}            
        }
    });


</script>
